<?php
define('YOURLS_API', true);
require_once( dirname(__FILE__).'/includes/load-yourls.php' );
require_once( YOURLS_INC.'/functions-infos.php' );
require_once( dirname(__FILE__).'/includes/userCake/config.php' );

yourls_maybe_require_auth();

$action = ( isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : null );

yourls_do_action( 'api', $action );
	
switch( $action ) {

    // Shorten a URL
    case 'shorturl':
        $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' );
        $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' );
        $title = ( isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : '' );
        $desc = ( isset( $_REQUEST['desc'] ) ? $_REQUEST['desc'] : '' );
        $image = ( isset( $_REQUEST['image'] ) ? $_REQUEST['image'] : '' );
        $return = yourls_add_new_link( $url, $keyword, $title, $desc, $image );
        $return['simple'] = ( isset( $return['shorturl'] ) ? $return['shorturl'] : '' ); // This one will be used in case output mode is 'simple'
        unset( $return['html'] ); // in API mode, no need for our internal HTML output
        break;

    // Get URL info
    case 'urlinfo':
        $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' );
        $return = get_url_info( $url );
        break;
    // Global stats
    case 'stats':
        $filter = ( isset( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : '' );
        $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : '' );
        $return = yourls_api_stats( $filter, $limit );
        break;

    // Stats for a shorturl
    case 'url-stats':
        $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
        $return = yourls_api_url_stats( $shorturl );
        break;

    // Expand a short link
    case 'expand':
        $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
        $return = yourls_api_expand( $shorturl );
        break;

    case 'popular':
        $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : 3 );
        if (!is_numeric($limit) || $limit<1 || $limit>40) $limit=3;
        $return = yourls_popular_links($limit);
        break;

    case 'last':
        $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : 3 );
        $time = ( isset( $_REQUEST['t'] ) ? $_REQUEST['t'] : null );
        if ($time!=null && !is_numeric($time)) $time=null;
        $return = yourls_last_links($limit,false,$time);
        break;

    case 'user':
        $mode =  ( isset( $_REQUEST['mode'] ) ? $_REQUEST['mode'] : "login" );
        $return = user_action($mode);
        break;

    case 'we':
        $return = contact_mail();
        break;

    case 'vote':
        $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : "" );
        $type = ( isset( $_REQUEST['type'] ) ? $_REQUEST['type'] : "" );
        $msg = ( isset( $_REQUEST['msg'] ) ? $_REQUEST['msg'] : "" );
        $return = vote_link($keyword, $type, $msg);
        break;

    // Missing or incorrect action parameter
    default:
        $return = array(
            'errorCode' => 400,
            'message'   => 'Unknown or missing "action" parameter',
            'simple'    => 'Unknown or missing "action" parameter',
        );
}

$format = ( isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : 'xml' );

yourls_api_output( $format, $return );

die();